<?php
// +----------------------------------------------------------------------
// | CoreThink [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.corethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com> <http://www.corethink.cn>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Think\Controller;
/**
 * 后台用户控制器
 * @author jry <598821125@qq.com>
 */
class CustomerServiceController extends AdminController{
	//客户服务记录列表
	public function index(){
		 //搜索
        $keyword = I('keyword', '', 'string');
        $condition = array('like','%'.$keyword.'%');
        $map['customerid|customer_service_id'] = array($condition, $condition,'_multi'=>true);
$keywords=I('keywords');
if(!empty($keywords)){
	$map['service_type']=$keywords;
}

$customerid=I('customerid');
if(!empty($customerid)){
	$map['customerid']=$customerid;
}

$projectid=I('projectid');
if(!empty($projectid)){
	$map['projectid']=$projectid;
}
        //获取所有用户
       $map['status'] = array('egt', '0'); //禁用和正常状态
        $data_list = D('CustomerService')->page(!empty($_GET["p"])?$_GET["p"]:1, C('ADMIN_PAGE_ROWS'))->where($map)->order('id desc')->select();
        $page = new \Common\Util\Page(D('CustomerService')->where($map)->count(), C('ADMIN_PAGE_ROWS' ));

			foreach($data_list as $key=>&$val){
			$maap['id']=$val['projectid'];//项目id
			$mapk['uid']=$val['customer_service_id'];//客服id
			$project=D('Project');
			$project_name=$project->where($maap)->field('project_number')->find();//项目名称
			$employee=D('Employee');
			$customer_service_name=$employee->where($mapk)->field('name')->find();//客服姓名
			$val['customer_service_id']=$customer_service_name['name'];
			$val['projectid']=$project_name['project_number'];
			$hh['tablename']='service_type';
			$hh['fieldvalue']=$val['service_type'];
			$service_type=D('Descriptions')->where($hh)->field('title')->find();
            $val['customerid']=get_customer_info($val['customerid'], 'customer_name');
			 $val['service_type']=$service_type['title'];
			 
			// $val['service_content']=get_str($val['service_content'],0,20).'<button type="button" class="btn btn-default" data-container="body" data-toggle="popover" data-placement="top" data-html="true" data-content="<h4>服务内容</h4>'.$val['service_content'].'<h4>服务结果</h4>'.$val['service_result'].'">更多</button>;
			$val['service_content']='<a data-container="body" data-toggle="popover" data-placement="top" data-html="true" data-content="<h4>服务内容</h4>'.$val['service_content'].'<h4>服务结果</h4>'.$val['service_result'].'">'.get_str($val['service_content'],0,20).'</a>';
			}
			$service_type=D('Descriptions')->where("tablename='service_type'")->select();
        //使用Builder快速建立列表页面。
        $builder = new \Common\Builder\ListBuilder();
        $builder->setMetaTitle('客户服务记录列表') //设置页面标题
                ->addTopButton('addnew')  //添加新增按钮
              //  ->addTopButton('resume')  //添加启用按钮
              //  ->addTopButton('forbid')  //添加禁用按钮
                ->addTopButton('delete')  //添加删除按钮
                ->addTopButton('self',array('title' => '我的服务','href' => U('Admin/customer_service/my') )) 
                ->setSearch('请输入客服ID/客户ID', U('index'),$service_type)
			     ->addTableColumn('projectid', '项目')

               ->addTableColumn('customer_service_id', '客服')
               ->addTableColumn('customerid', '客户')

				->addTableColumn('service_type','服务类型')
				->addTableColumn('service_content','服务内容')
          ->addTableColumn('service_time', '服务时间','time')
           // ->addTableColumn('customerid', '客户ID')
			//	->addTableColumn('processor','执行人')
			//	->addTableColumn('service_result','服务结果')
			//	->addTableColumn('inputer','录入者')
			//	->addTableColumn('feedback','客户反馈')
            //    ->addTableColumn('remark', '备注') 
				 
               
     //           ->addTableColumn('status', '状态', 'status')
                ->addTableColumn('right_button', '操作', 'btn')
                ->setTableDataList($data_list) //数据列表
                ->setTableDataPage($page->show()) //数据列表分页
                ->addRightButton('edit')   //添加编辑按钮
          //      ->addRightButton('forbid') //添加禁用/启用按钮
                ->addRightButton('delete') //添加删除按钮
                ->display();
	}
//新增
	public function add(){
		$service_type=D('Descriptions')->where('tablename="service_type"')->field('fieldvalue as id,title')->select();
		$id=is_login();
		$username=get_user_info($id);
		if(IS_POST){
            $customer_object = D('CustomerService');
            $data = $customer_object->create();
            if($data){
                $id = $customer_object->add();
                if($id){
                    $this->success('新增成功', U('index'));
                }else{
                    $this->error('新增失败');
                }
            }else{
                $this->error($customer_object->getError());
            }
        }else{
            //使用FormBuilder快速建立表单页面。
            $kefu_arr=M("employee")->where("department=7")->field("uid as id,name as title")->select();
			$customerid = I("customerid");
			$projectid=I('projectid');
			if(empty($customerid)){
				$customerid=D('Project')->where("id='$projectid'")->getField('customerid');
			}
			if(empty($projectid)){
				$projectid='00';
			}
             $customer_name = get_customer_info($customerid,'customer_name');
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('新增客户服务记录') //设置页面标题
                         ->setPostUrl(U('add')) //设置表单提交地址
				       
						->addFormItem('customerid','customer_name', '客户ID', '客户姓名：')
						->addFormItem('customer_service_id','select2', '客服编号', '客服ID',$kefu_arr)
						->addFormItem('projectid','project', '项目编号', '项目ID')
						->addFormItem('service_type','select2','服务类型','服务类型',$service_type)
						->addFormItem('inputer','hidden','录入者','录入者')
						->addFormItem('service_content','textarea','服务内容','服务内容')
						->addFormItem('service_result','textarea','服务结果','服务结果')
						//->addFormItem('feedback','textarea', '客户反馈', '客户反馈')
						//->addFormItem('remark','textarea','备注','备注')
				//->addFormItem('processor','text','执行人','执行人')
				//	->addFormItem('service_time','radio','服务时间','服务时间')
				//   ->addFormItem('vip', 'radio', 'VIP等级', 'VIP等级', C('USER_VIP_LEVEL'))
				->setFormData(array('inputer' =>$username['username'],'customerid'=>$customerid,'projectid'=>$projectid,'customer_service_id'=>is_login()))  
			          ->display();
        }
		
}

 /**
     * 编辑
     * @author jry <598821125@qq.com>
     */
    public function edit($id){
    			 
				$service_type=D('Descriptions')->where('tablename="service_type"')->field('fieldvalue as id,title')->select();
				 $kefu_arr=M("employee")->where("department=7")->field("uid as id,name as title")->select();
        //获取信息
        $info = D('CustomerService')->find($id);

        if(IS_POST){
            $customer_object = D('CustomerService');
             
            if($customer_object->save($_POST)){
                $this->success('更新成功', U('index'));
            }else{
                $this->error('更新失败', $customer_object->getError());
            }
        }else{
        	$map['id']=$id;	
            $info = D('CustomerService')->find($id);
            //使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('编辑') //设置页面标题
                    ->setPostUrl(U('edit')) //设置表单提交地址
                    ->addFormItem('id', 'hidden', 'ID', 'ID')
                 //   ->addFormItem('usertype', 'radio', '用户类型', '用户类型', C('USER_TYPE_LIST'))
                    ->addFormItem('customerid','user_list', '客户', '',$map)
		            ->addFormItem('customer_service_id','select2', '客服', '客服ID',$kefu_arr)
		            ->addFormItem('projectid','text', '项目编号', '项目ID')
				//	->addFormItem('service_time','radio','服务时间','服务时间')
					->addFormItem('service_type','select2','服务类型','服务类型',$service_type)
					->addFormItem('inputer','text','录入者','录入者')
					->addFormItem('service_content','textarea','服务内容','服务内容')
				//	->addFormItem('processor','text','执行人','执行人')
					->addFormItem('service_result','textarea','服务结果','服务结果')
		       //     ->addFormItem('feedback','textarea', '客户反馈', '客户反馈') 
				//	->addFormItem('remark','textarea','备注','备注')  

                    ->setFormData($info)
                    ->display();
        }
    }

public function my(){
    
	$uid=is_login();
	 //搜索
        $keyword = I('keyword', '', 'string');
        $condition = array('like','%'.$keyword.'%');
        if($condition){
        $map['customerid|customer_service_id'] = array($condition, $condition,'_multi'=>true);
        }
		$map['customer_service_id|inputer']=array($uid, $uid,'_multi'=>true);
       
		$keywords=I('keywords');
if(!empty($keywords)){
	$map['service_type']=$keywords;
}
	$data_list = D('CustomerService')->page(!empty($_GET["p"])?$_GET["p"]:1, C('ADMIN_PAGE_ROWS'))->where($map)->order('id desc')->select();
        $page = new \Common\Util\Page(D('CustomerService')->where($map)->count(), C('ADMIN_PAGE_ROWS' ));

			foreach($data_list as $key=>&$val){
			$map['id']=$val['projectid'];//项目id
			$mapk['uid']=$val['customer_service_id'];//客服id
			$project=D('Project');
			$project_name=$project->where($map)->field('project_name')->find();//项目名称
			$employee=D('Employee');
			$customer_service_name=$employee->where($mapk)->field('name')->find();//客服姓名
			$val['customer_service_id']=$customer_service_name['name'];
			$val['projectid']=$project_name['project_name'];
			$hh['tablename']='service_type';
			$hh['fieldvalue']=$val['service_type'];
			$service_type=D('Descriptions')->where($hh)->field('title')->find();
            
			 $val['service_type']=$service_type['title'];
			}
			$service_type=D('Descriptions')->where("tablename='service_type'")->select();
        //使用Builder快速建立列表页面。
        $builder = new \Common\Builder\ListBuilder();
        $builder->setMetaTitle('客户服务记录列表') //设置页面标题
            ->addTopButton('addnew')  //添加新增按钮
		//	->addTopButton('resume')  //添加启用按钮
		//	->addTopButton('forbid')  //添加禁用按钮
			->addTopButton('delete')  //添加删除按钮
			
		//	->addTopButton('self',array('title' => '我的任务','href' => U('Admin/task/my') )) 
            ->addTopButton('self',array('title' => '我的服务','href' => U('Admin/customer_service/my') )) 
         //   ->addTopButton('self',array('title' => '我的项目','href' => U('Admin/project/my') )) 
			
			
			
			->setSearch('请输入客服ID/客户ID', U('my'),$service_type)
			->addTableColumn('projectid', '项目')
			->addTableColumn('customer_service_id', '客服')
			->addTableColumn('service_type','服务类型')
			->addTableColumn('service_content','服务内容')
			->addTableColumn('service_time', '服务时间','time')
			// ->addTableColumn('customerid', '客户ID')
			//	->addTableColumn('processor','执行人')
			//	->addTableColumn('service_result','服务结果')
			//	->addTableColumn('inputer','录入者')
			//	->addTableColumn('feedback','客户反馈')
			//    ->addTableColumn('remark', '备注')
			//           ->addTableColumn('status', '状态', 'status')
			->addTableColumn('right_button', '操作', 'btn')
			->setTableDataList($data_list) //数据列表
			->setTableDataPage($page->show()) //数据列表分页
			->addRightButton('edit')   //添加编辑按钮
			//      ->addRightButton('forbid') //添加禁用/启用按钮
			->addRightButton('delete') //添加删除按钮
			->display();
}



























}